package com.zzptc.easyshop.control;

import com.zzptc.easyshop.dao.IDAO;
import com.zzptc.easyshop.dao.UserDao;
import com.zzptc.easyshop.factory.DaoFactoryImp;
import com.zzptc.easyshop.vo.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet(name = "LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter pw=response.getWriter();
      //  pw.print("login succ");
        //接收用户名和密码
        String username=request.getParameter("userName");
        String pwd=request.getParameter("passWord");
        //将接收到的信息放入VO   JAVABEAN
        Users user=new Users();
        user.setEu_user_name(username);
        user.setEu_password(pwd);

        //调用dao中的登陆方法
      // IDAO userdao=new UserDao();//new  工类来实现单例模式
        IDAO userdao= null;
        try {
            userdao = DaoFactoryImp.getInstance().createDAO("userdao");
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean flag=false;
        try {
           flag= ((UserDao) userdao).login(user);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(flag){
            pw.print("login succ welcome");
            //转发到登陆成功的页面  重定向到新的页面
        }else{
            pw.print("login error");
        }

    }
}
